package com.example.demo.service;

import com.example.demo.dao.XhsDao;
import com.example.demo.entity.TxhsVirtualshop;
import com.example.demo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;
import java.util.Optional;

@Service
public class XhsService {
    @Autowired
    private XhsDao userRepository;

    public List<TxhsVirtualshop> getAllUsers() {
        return userRepository.findAll();
    }

    public boolean DeleteById(Integer id) {
        userRepository.delectxhsVirtualshop(id);
        //TODO:实现
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    public Result<TxhsVirtualshop> updateXhsvirtual(TxhsVirtualshop hsVirtualsho) {
        try {
            Optional<TxhsVirtualshop> existingProductOptional = userRepository.findById(hsVirtualsho.getId());
            if (existingProductOptional.isPresent()) {
                TxhsVirtualshop x = existingProductOptional.get();
                x.setShop_name(hsVirtualsho.getShop_name());
                x.setShop_type(hsVirtualsho.getShop_type());
                TxhsVirtualshop a = userRepository.save(x);
                return Result.success(a);
            } else {
                return Result.successflas(null, "产品不存在，无法更新");
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.successflas(null, "更新产品时出现内部错误：" + e.getMessage());
        }
    }

    @Transactional
    public boolean addXhlshop(String customer_id, String shopType, String shopName, int beReal) {
        userRepository.addXhsvirtualshop(customer_id, shopType, beReal, shopName);
        //TODO:实现
        return true;
    }

}